package com.bw.task.fragment.onefragment;

import android.content.Intent;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;

import android.view.View;

import android.widget.ImageView;

import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bw.library.mvp.view.BaseFragment;
import com.bw.task.R;
import com.bw.task.activity.ItemImg;
import com.bw.task.bean.ImgBean;
import com.bw.task.bean.MoneyBean;
import com.bw.task.bean.PopupWindioBean;
import com.bw.task.bean.UserBean;
import com.bw.task.mvp.contern.UserContent;
import com.bw.task.mvp.model.UserModel;
import com.bw.task.mvp.persenter.UserPersenter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Response;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import retrofit2.Callback;
import retrofit2.http.HTTP;


public class  Fragment1 extends BaseFragment<UserPersenter> implements UserContent.mView, OnItemChildClickListener {

    private static final String TAG = "Fragment1";
    private RecyclerView recyclerView;
    List<ImgBean.ResBean.VerticalBean> vertical;
    ImageView img;
    MyimgAdapter myimgAdapter;
    ExecutorService executorService = Executors.newSingleThreadExecutor();

    @Override
    public int bandLayout() {
        return R.layout.fag_1;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        mpresenter=new UserPersenter(this,new UserModel());
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        mpresenter.getimg(2+"");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "1onDestroy: ");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "1onResume: ");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "1onDestroyView: ");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "1onPause: ");
    }

    @Override
    public void UserBean(UserBean userBean) {

    }

    @Override
    public void LogUserBean(UserBean userBean) {

    }

    @Override
    public void MoneyUserBean(MoneyBean userBean) {

    }

    @Override
    public void getImgBean(ImgBean imgBean) {

        vertical = imgBean.getRes().getVertical();

        myimgAdapter = new MyimgAdapter(R.layout.item, vertical);
        recyclerView.setAdapter(myimgAdapter);
        myimgAdapter.setOnItemChildClickListener(this);
//        myimgAdapter.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
////                ToastUtils.showShort(""+position);
//                switch (view.getId()){
//                    case R.id.item_love:
//                        ToastUtils.showShort("心"+position);
//                        break;
//                }
//            }
//        });
    }



    boolean islove=true;
    @Override
    public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
        switch (view.getId()){
            case R.id.item_love:

                myimgAdapter.notifyDataSetChanged();
                break;
            case R.id.item_img:
                Intent intent = new Intent(getActivity(), ItemImg.class);
                intent.putExtra("img",vertical.get(position).getImg());
                startActivity(intent);
                break;
            case R.id.item_imgtx:
                ToastUtils.showShort("头像");
                String s = "/download";
                OkGo.<File>get(vertical.get(position).getImg())
                        .execute(new FileCallback(s) {
                            @Override
                            public void onSuccess(Response<File> response) {
                                Log.i("randomcode","下载成功1 " + response.body()+vertical.get(position).getImg());
                            }

                            @Override
                            public void onError(Response<File> response) {
                                super.onError(response);
                                Log.i("randomcode","下载失败1 " + response.body()+vertical.get(position).getImg());
                            }
                        });
                break;
        }
    }


    public class MyimgAdapter extends BaseQuickAdapter<ImgBean.ResBean.VerticalBean, BaseViewHolder> {

        public MyimgAdapter(int layoutResId, @Nullable List<ImgBean.ResBean.VerticalBean> data) {
            super(layoutResId, data);
            addChildClickViewIds(R.id.item_love);
            addChildClickViewIds(R.id.item_img);
            addChildClickViewIds(R.id.item_imgtx);

        }

        @Override
        protected void convert(@NotNull BaseViewHolder baseViewHolder, ImgBean.ResBean.VerticalBean verticalBean) {
            Glide.with(getContext()).load(verticalBean.getImg()).into((ImageView) baseViewHolder.getView(R.id.item_img));
            Glide.with(getContext()).load(verticalBean.getImg()).transform(new CircleCrop()).into((ImageView) baseViewHolder.getView(R.id.item_imgtx));
            baseViewHolder.setText(R.id.item_num,verticalBean.getRank()+"");

            //local_icon_like
            /*CheckBox view = baseViewHolder.getView(R.id.item_love);
            view.setButtonDrawable(R.mipmap.local_icon_like);*/

            img = baseViewHolder.getView(R.id.item_love);
            verticalBean.setDrobe(R.mipmap.local_icon_unlike);
            Glide.with(getContext()).load(verticalBean.getDrobe()).into((ImageView) baseViewHolder.getView(R.id.item_love));


            ImageView view = baseViewHolder.getView(R.id.item_love);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (islove){
                        verticalBean.setDrobe(R.mipmap.local_icon_like);
                        Glide.with(getContext()).load(verticalBean.getDrobe()).into((ImageView) baseViewHolder.getView(R.id.item_love));
                        islove=false;
                    }else {
                        verticalBean.setDrobe(R.mipmap.local_icon_unlike);
                        Glide.with(getContext()).load(verticalBean.getDrobe()).into((ImageView) baseViewHolder.getView(R.id.item_love));
                        islove=true;

                    }
                }
            });
        }
    }

    @Override
    public void showToast() {

    }
}